﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApFLEXAttachment
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.Flexibles;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public abstract class CApFLEXAttachment : INotifyPropertyChanged
{
  private double m_entryFulcrum;
  private double m_exitFulcrum;
  private ITxLocatableObject m_startFixedFrame;
  private ITxLocatableObject m_endFixedFrame;
  private double m_endLengthFromStart;

  protected CApFLEXAttachment(ICApCableExtentDescriptor cableExtentDescriptor)
  {
    this.m_startFixedFrame = cableExtentDescriptor.StartFrame;
    this.m_endFixedFrame = cableExtentDescriptor.EndFrame;
    this.m_endLengthFromStart = cableExtentDescriptor.Length;
    this.IndexFixedFrame = 0;
  }

  protected CApFLEXAttachment(ITxCable cable)
  {
    this.m_startFixedFrame = cable.StartFrame;
    this.m_endFixedFrame = cable.EndFrame;
    this.m_endLengthFromStart = cable.CableData.Length;
    this.IndexFixedFrame = 0;
  }

  public abstract TxCableAttachment Attachment { get; }

  public virtual bool DoesUseFrame(ITxLocatableObject frame)
  {
    return frame != null && frame == this.FixedFrame;
  }

  public int IndexFixedFrame { get; set; }

  public CApFLEXAttachmentType AttachmentType
  {
    get
    {
      CApFLEXAttachmentType attachmentType = CApFLEXAttachmentType.Fixed;
      if (this.Attachment is TxCableFixedAttachment)
        attachmentType = CApFLEXAttachmentType.Fixed;
      else if (this.Attachment is TxCableRotatingAttachment)
        attachmentType = CApFLEXAttachmentType.Rotating;
      else if (this.Attachment is TxCableRotatingSlidingAttachment)
        attachmentType = CApFLEXAttachmentType.RotatingSliding;
      else if (this.Attachment is TxCableRetractSystemAttachmentType1)
        attachmentType = CApFLEXAttachmentType.RetractSystemType1;
      else if (this.Attachment is TxCableRetractSystemAttachmentType2)
        attachmentType = CApFLEXAttachmentType.RetractSystemType2;
      else if (this.Attachment is TxCableRetractSystemAttachmentType3)
        attachmentType = CApFLEXAttachmentType.RetractSystemType3;
      return attachmentType;
    }
  }

  public virtual CApFLEXAttachmentLocation Location
  {
    get => (CApFLEXAttachmentLocation) this.Attachment.Location;
  }

  public double LengthFromStart
  {
    get
    {
      switch (this.Location)
      {
        case CApFLEXAttachmentLocation.AlongCable:
          return this.Attachment.LengthFromStart;
        case CApFLEXAttachmentLocation.Start:
          return 0.0;
        case CApFLEXAttachmentLocation.End:
          return this.m_endLengthFromStart;
        default:
          throw new NotImplementedException();
      }
    }
    set
    {
      if (this.Location == CApFLEXAttachmentLocation.AlongCable)
        this.Attachment.LengthFromStart = value;
      else if (this.Location == CApFLEXAttachmentLocation.End)
        this.m_endLengthFromStart = value;
      this.NotifyPropertyChanged(nameof (LengthFromStart));
      this.NotifyPropertyChanged("StringAttachmentLength");
    }
  }

  public double AttachmentEntryFulcrumLength
  {
    get => this.m_entryFulcrum;
    set
    {
      this.m_entryFulcrum = value;
      this.NotifyPropertyChanged(nameof (AttachmentEntryFulcrumLength));
      this.NotifyPropertyChanged("StringAttachmentEntryFulcrumLength");
    }
  }

  public double AttachmentExitFulcrumLength
  {
    get => this.m_exitFulcrum;
    set
    {
      this.m_exitFulcrum = value;
      this.NotifyPropertyChanged(nameof (AttachmentExitFulcrumLength));
      this.NotifyPropertyChanged("StringAttachmentExitFulcrumLength");
    }
  }

  public string StringAttachmentLength
  {
    get
    {
      return (this.LengthFromStart / TxApplication.Options.Units.LinearMultiplier).ToString(CApCableUtility.NumericLinearFormat);
    }
    set
    {
      this.LengthFromStart = Convert.ToDouble(value) * TxApplication.Options.Units.LinearMultiplier;
      this.NotifyPropertyChanged("LengthFromStart");
      this.NotifyPropertyChanged(nameof (StringAttachmentLength));
    }
  }

  public string StringAttachmentEntryFulcrumLength
  {
    get
    {
      return (this.AttachmentEntryFulcrumLength / TxApplication.Options.Units.LinearMultiplier).ToString(CApCableUtility.NumericLinearFormat);
    }
    set
    {
      this.AttachmentEntryFulcrumLength = Convert.ToDouble(value) * TxApplication.Options.Units.LinearMultiplier;
      this.NotifyPropertyChanged("AttachmentEntryFulcrumLength");
      this.NotifyPropertyChanged(nameof (StringAttachmentEntryFulcrumLength));
    }
  }

  public string StringAttachmentExitFulcrumLength
  {
    get
    {
      return (this.AttachmentExitFulcrumLength / TxApplication.Options.Units.LinearMultiplier).ToString(CApCableUtility.NumericLinearFormat);
    }
    set
    {
      this.AttachmentExitFulcrumLength = Convert.ToDouble(value) * TxApplication.Options.Units.LinearMultiplier;
      this.NotifyPropertyChanged("AttachmentExitFulcrumLength");
      this.NotifyPropertyChanged(nameof (StringAttachmentExitFulcrumLength));
    }
  }

  public string StringAttachmentIndex => this.IndexFixedFrame.ToString();

  public static string AttachmentTypeToString(CApFLEXAttachmentType attachmentType)
  {
    string str;
    switch (attachmentType)
    {
      case CApFLEXAttachmentType.Fixed:
        str = CApFLEXStringTable.AttachmentGridLabelFixed;
        break;
      case CApFLEXAttachmentType.Rotating:
        str = CApFLEXStringTable.AttachmentGridLabelRotating;
        break;
      case CApFLEXAttachmentType.RotatingSliding:
        str = CApFLEXStringTable.AttachmentGridLabelRotatingSliding;
        break;
      case CApFLEXAttachmentType.RetractSystemType1:
        str = CApFLEXStringTable.AttachmentGridLabelRetractSystemType1;
        break;
      case CApFLEXAttachmentType.RetractSystemType2:
        str = CApFLEXStringTable.AttachmentGridLabelRetractSystemType2;
        break;
      case CApFLEXAttachmentType.RetractSystemType3:
        str = CApFLEXStringTable.AttachmentGridLabelRetractSystemType3;
        break;
      default:
        str = "";
        break;
    }
    return str;
  }

  public string StringAttachmentType
  {
    get => CApFLEXAttachment.AttachmentTypeToString(this.AttachmentType);
  }

  public ITxLocatableObject FixedFrame
  {
    get
    {
      switch (this.Location)
      {
        case CApFLEXAttachmentLocation.AlongCable:
          return this.Attachment.FixedFrame;
        case CApFLEXAttachmentLocation.Start:
          return this.m_startFixedFrame;
        case CApFLEXAttachmentLocation.End:
          return this.m_endFixedFrame;
        default:
          throw new NotImplementedException();
      }
    }
    set
    {
      switch (this.Location)
      {
        case CApFLEXAttachmentLocation.AlongCable:
          this.Attachment.FixedFrame = value;
          break;
        case CApFLEXAttachmentLocation.Start:
          this.m_startFixedFrame = value;
          break;
        case CApFLEXAttachmentLocation.End:
          this.m_endFixedFrame = value;
          break;
        default:
          throw new NotImplementedException();
      }
      this.NotifyPropertyChanged(nameof (FixedFrame));
      this.NotifyPropertyChanged("FixedFrameName");
    }
  }

  public string FixedFrameName
  {
    get
    {
      string fixedFrameName = "";
      if (this.FixedFrame != null)
        fixedFrameName = ((ITxObject) this.FixedFrame).Name;
      return fixedFrameName;
    }
  }

  public virtual ITxLocatableObject ClosestFrameToStart => this.FixedFrame;

  public virtual double LowerLengthFromStart => this.LengthFromStart;

  public virtual double RetractionCapability => 0.0;

  public static bool operator <(CApFLEXAttachment attachment1, CApFLEXAttachment attachment2)
  {
    return attachment1.LengthFromStart <= attachment2.LengthFromStart;
  }

  public static bool operator >(CApFLEXAttachment attachment1, CApFLEXAttachment attachment2)
  {
    return attachment1.LengthFromStart > attachment2.LengthFromStart;
  }

  public override bool Equals(object obj)
  {
    return obj != null && this.GetType().Equals(obj.GetType()) && ((CApFLEXAttachment) obj).LengthFromStart == this.LengthFromStart;
  }

  public static bool AreAttachmentFramesAttachedToRobot(
    ITxObject robot,
    List<CApFLEXAttachment> attachmentList)
  {
    bool robot1 = true;
    foreach (CApFLEXAttachment attachment in attachmentList)
    {
      ITxObject robotFrame = (ITxObject) null;
      CApCableUtility.ComputeRobotFrameAttachement(attachment.FixedFrame, out robotFrame);
      if (robotFrame != robot)
      {
        robot1 = false;
        break;
      }
    }
    return robot1;
  }

  public static List<CApFLEXAttachment> CreateAttachmentsFromCable(ITxCable cable)
  {
    List<CApFLEXAttachment> attachmentsFromCable = new List<CApFLEXAttachment>();
    foreach (TxCableAttachment attachment in cable.CableData.Attachments)
    {
      CApFLEXAttachment capFlexAttachment;
      switch (attachment)
      {
        case TxCableFixedAttachment fixedAttachment:
          capFlexAttachment = (CApFLEXAttachment) new CApFLEXFixedAttachment(cable, fixedAttachment);
          break;
        case TxCableRotatingAttachment rotatingAttachment:
          capFlexAttachment = (CApFLEXAttachment) new CApFLEXRotatingAttachment(cable, rotatingAttachment);
          break;
        case TxCableRotatingSlidingAttachment rotatingSlidingAttachment:
          capFlexAttachment = (CApFLEXAttachment) new CApFLEXRotatingSlidingAttachment(cable, rotatingSlidingAttachment);
          break;
        case TxCableRetractSystemAttachment _:
          switch (attachment)
          {
            case TxCableRetractSystemAttachmentType1 retractSystemAttachmentType1:
              capFlexAttachment = (CApFLEXAttachment) new CApFLEXRetractSystemAttachmentType1(cable, retractSystemAttachmentType1);
              break;
            case TxCableRetractSystemAttachmentType2 retractSystemAttachmentType2:
              capFlexAttachment = (CApFLEXAttachment) new CApFLEXRetractSystemAttachmentType2(cable, retractSystemAttachmentType2);
              break;
            case TxCableRetractSystemAttachmentType3 retractSystemAttachmentType3:
              capFlexAttachment = (CApFLEXAttachment) new CApFLEXRetractSystemAttachmentType3(cable, retractSystemAttachmentType3);
              break;
            default:
              throw new NotImplementedException();
          }
          break;
        default:
          throw new NotImplementedException();
      }
      attachmentsFromCable.Add(capFlexAttachment);
    }
    return attachmentsFromCable;
  }

  public static CApFLEXAttachment.AttachmentFilter FilterExcludingType(CApFLEXAttachmentType type)
  {
    CApFLEXAttachment.AttachmentFilter attachmentFilter = new CApFLEXAttachment.AttachmentFilter();
    attachmentFilter.IncludeAllTypes();
    attachmentFilter.ExcludeType(type);
    return attachmentFilter;
  }

  public event PropertyChangedEventHandler PropertyChanged;

  protected void NotifyPropertyChanged(string info)
  {
    PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
    if (propertyChanged == null)
      return;
    propertyChanged((object) this, new PropertyChangedEventArgs(info));
  }

  public class AttachmentFilter
  {
    public AttachmentFilter() => this.FilteredTypes = new List<CApFLEXAttachmentType>();

    private List<CApFLEXAttachmentType> FilteredTypes { get; set; }

    public bool ContainsType(CApFLEXAttachmentType type) => this.FilteredTypes.Contains(type);

    public void IncludeType(CApFLEXAttachmentType type)
    {
      if (this.FilteredTypes.Contains(type))
        return;
      this.FilteredTypes.Add(type);
    }

    public void IncludeAllTypes()
    {
      this.FilteredTypes.Clear();
      foreach (CApFLEXAttachmentType flexAttachmentType in Enum.GetValues(typeof (CApFLEXAttachmentType)))
        this.FilteredTypes.Add(flexAttachmentType);
    }

    public void ExcludeType(CApFLEXAttachmentType type) => this.FilteredTypes.Remove(type);

    public void ExcludeAllTypes() => this.FilteredTypes.Clear();
  }
}
